<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
    public $_table;
    
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Get one record
     * @author luongvu
     * @param $id integer primary key of record
     */
    public function get($id) {
        if (empty ($id)) {
            return false;
        }
        $this->db->where('id', $id);
        return $this->db->get($this->_table);
    }
    
    /**
     * Get all record
     * @author luongvu
     * @param $where string where clause, default null
     * @param $limit string limit clause, default null
     * @param $orderby string order by clause, default null
     */
    public function get_all($where = null, $limit = null, $orderby = null) {
    	
        if (! empty($where)) {
            $this->db->where($where);
        }
        
        if (! empty($limit)) {
            $this->db->limit($limit);
        }
        
        if (! empty($orderby)) {
            $this->db->order_by($orderby);
        }
        
        return $this->db->get($this->_table);
    }
    
    /**
     * Insert a record
     * @author luongvu
     * @param $data array array data you want to insert
     */
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    /**
     * Update a record
     * @author luongvu
     * @param $data array array data you want to update
     * @param $id integer primary key
     */
    public function update($data, $id) {
        if (empty($id) || ! is_numeric($id)) {
            return false;
        }
        $this->db->where('id', $id);
        $this->db->update($this->_table, $data);
    }
    
    
    /**
     * Delete a record
     * @author luongvu
     * @param $id integer primary key
     */
    public function delete($id) {
        if (empty($id) || ! is_numeric($id)) {
            return false;
        }
        $this->db->where('id', $id);
        $this->db->delete($this->_table);
    }
    
    public function count_all($where = '') {
    	if ($where) {
            foreach ($where as $key => $val) {
                $this->db->where($key, $val);
            }
    	}
    	return $this->db->count_all_results($this->_table);
    }
    
    public function get_limit($limit, $page, $where = '') {
    	//$this->db->cache_on();
    	$start = $page ? $limit * ($page - 1) : 0;
    	
        if ($where && is_array($where)) {
            foreach ($where as $key => $val) {
                $this->db->where($key, $val);
            }
        }
        
    	return $this->db->get($this->_table, $limit, $start);
    }
    
    public function get_offset_limit($limit, $offset, $where = '') {
        if ($where && is_array($where)) {
            foreach ($where as $key => $val) {
                $this->db->where($key, $val);
            }
        }
        
    	return $this->db->get($this->_table, $limit, $offset);
    }
    
    public function get_where($where) {
        return $this->db->get_where($this->_table, $where);
    }
    
    public function get_limit_offset($limit, $offset) { 
    	return $this->db->get($this->_table, $limit, $offset);
    }
}
